package mapper

import (
	"github.com/beego/beego/v2/client/orm"
	"ksd-social-api/modules/resume/model"
)

type ResumeMapper struct{}

/**
 * @desc 根据类型查询广告
 * @author feige
 * @date 2023-11-14
 * @version 1.0
 * SELECT * FROM xk_resume WHERE `status` = 1 AND is_deleted = 0  AND type = 2 AND start_time <=NOW() AND end_time >=NOW() ORDER BY sorted ASC;
 */
func (service *ResumeMapper) FindResumeByUserId(userId uint64, systemId uint64) []*model.Resume {
	// 1：准备容器对象，开始装载数据库数据
	resumeList := []*model.Resume{}
	// 2: 创建一个数据连接
	mysql := orm.NewOrm()
	// 3: 指定查询的表
	qs := mysql.QueryTable("xk_resume")
	// 4: 创建查询条件
	whereCondition := orm.NewCondition()
	whereCondition = whereCondition.
		And("is_deleted", 0).
		And("system_id", systemId)
	// 除了平台作者本身只查自身
	if userId > 1 {
		whereCondition = whereCondition.And("user_id", userId)
	}
	// 4: 执行查询
	_, err := qs.SetCond(whereCondition).OrderBy("-create_time").All(&resumeList)
	if nil != err {
		return nil
	}
	return resumeList
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询小册信息
 */
func (mapper *ResumeMapper) GetById(id uint64, systemId uint64) (resume *model.Resume, err error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 2: 赋予id给结构体
	dbResume := model.Resume{Id: id, SystemId: systemId}
	// 3: 读取id的数据放入到结构体，进行返回
	err = db.Read(&dbResume, "id", "system_id")
	// 4: 结构返回
	return &dbResume, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询小册信息
 */
func (mapper *ResumeMapper) GetByIdAndUesrId(id uint64, userId uint64, systemId uint64) (resume *model.Resume, err error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 2: 赋予id给结构体
	dbResume := model.Resume{Id: id, UserId: userId, SystemId: systemId}
	// 3: 读取id的数据放入到结构体，进行返回
	err = db.Read(&dbResume, "id", "user_id", "system_id")
	// 4: 结构返回
	return &dbResume, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询小册信息
 */
func (mapper *ResumeMapper) CountByUserId(userId uint64, systemId uint64) int {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 3: 读取id的数据放入到结构体，进行返回
	var count int
	db.Raw("select count(1) from xk_resume where user_id = ? and system_id = ?", userId, systemId).QueryRow(&count)
	// 4: 结构返回
	return count
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询小册信息
 */
func (mapper *ResumeMapper) CountUserBuyResumeNo(userId uint64, systemId uint64) int {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 3: 读取id的数据放入到结构体，进行返回
	var count int
	db.Raw("select count(1) from xk_resume where user_id = ? and system_id = ?", userId, systemId).QueryRow(&count)
	// 4: 结构返回
	return count
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询小册信息
 */
func (mapper *ResumeMapper) CountUserBuyResumeIdNo(userId uint64, resumeId uint64, systemId uint64) int {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 3: 读取id的数据放入到结构体，进行返回
	var count int
	db.Raw("select count(1) from xk_resume where id = ? and user_id = ? and system_id = ?", resumeId, userId, systemId).QueryRow(&count)
	// 4: 结构返回
	return count
}

/**
 * @desc 更新状态
 * @author feige
 * @date 2023-11-14
 * @version 1.0
 */
func (mapper *ResumeMapper) UpdateStatus(id uint64, status int, systemId uint64) bool {
	newOrm := orm.NewOrm()
	_, err := newOrm.Raw("update xk_resume set status = ?,update_time = now()  where id = ? and system_id = ?", status, id, systemId).Exec()
	if err != nil {
		return false
	}
	return true
}

/**
 * @desc 更新状态
 * @author feige
 * @date 2023-11-14
 * @version 1.0
 */
func (mapper *ResumeMapper) UpdateResumePay(id uint64, payMethod string, tradeNo string, systemId uint64) bool {
	newOrm := orm.NewOrm()
	_, err := newOrm.Raw("update xk_resume set status = 2,pay_method = ?, tradeno=?, update_time = now()  where id = ? and system_id = ?", payMethod, tradeNo, id, systemId).Exec()
	if err != nil {
		return false
	}
	return true
}

/**
 * @desc 更新状态
 * @author feige
 * @date 2023-11-14
 * @version 1.0
 */
func (mapper *ResumeMapper) UpdateResumePayByUserId(userId uint64, systemId uint64) bool {
	newOrm := orm.NewOrm()
	_, err := newOrm.Raw("update xk_resume set status = 2, update_time = now()  where user_id = ? and system_id = ? and status = 1", userId, systemId).Exec()
	if err != nil {
		return false
	}
	return true
}

/**
 * @desc 更新文件
 * @author feige
 * @date 2023-11-14
 * @version 1.0
 */
func (mapper *ResumeMapper) UpdateResumeNewFile(id uint64, file string, systemId uint64) bool {
	newOrm := orm.NewOrm()
	_, err := newOrm.Raw("update xk_resume set status = 3,resume_new_file = ?,update_time = now()  where id = ? and system_id = ?", file, id, systemId).Exec()
	if err != nil {
		return false
	}
	return true
}

/*
*保存笔记
 */
func (mapper *ResumeMapper) SaveUpdateResume(resume model.Resume) uint64 {
	newOrm := orm.NewOrm()
	_, _ = newOrm.InsertOrUpdate(&resume)
	return resume.Id
}

/*
*保存笔记
 */
func (mapper *ResumeMapper) UpdateResume(resume *model.Resume) uint64 {
	db := orm.NewOrm()
	_, _ = db.Raw("update xk_resume set  status=2,pay_method= ?,trade_no = ?,code = ?,update_time = now()  where id = ? and system_id = ?", resume.PayMethod, resume.TradeNo, resume.Code, resume.Id, resume.SystemId).Exec()
	return resume.Id
}
